home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-01
/
cuj9205.zip
/
1005016A
< prev
next >
Wrap
Text File
|
1992-06-02
|
639b
|
33 lines
Listing 3 -- strtol.c
/* strtol function */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
long (strtol)(const char *s, char **endptr, int base)
{ /* convert string to long, with checking */
const char *sc;
unsigned long x;
for (sc = s; isspace(*sc); ++sc)
;
x = _Stoul(s, endptr, base); /* not sc! */
if (*sc == '-' && x <= LONG_MAX)
{ /* negative number overflowed */
errno = ERANGE;
return (LONG_MIN);
}
else if (*sc != '-' && LONG_MAX < x)
{ /* positive number overflowed */
errno = ERANGE;
return (LONG_MAX);
}
else
return ((long)x);
}